/*
 * @(#)RequestParameterInterceptor.java $version 2010. 8. 11
 *
 * Copyright 2007-2010 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nhncorp.usf.core.interceptor.impl;

import java.util.Enumeration;
import java.util.Map;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.nhncorp.lucy.web.helper.ServletHelper;
import com.nhncorp.lucy.web.interceptor.AbstractInterceptor;
import com.nhncorp.usf.core.util.ServletRequestUtil;
import com.nhncorp.usf.core.util.StringUtil;
import com.nhncorp.usf.core.xwork.ActionInvocationHelper;
import com.opensymphony.xwork.ActionInvocation;

/**
 * @author Web Platform Development Team
 */
public class RequestParameterInterceptor extends AbstractInterceptor implements InterceptorPropertyAware {
	private static final long serialVersionUID = 8248793584172935597L;

	Log log = LogFactory.getLog(CookieHandleInterceptor.class);

    private Properties properties;

    enum DEFAULT_PARAM_NAMES {
        usf_querystring, usf_uri, usf_url
    }

    private boolean enableQueryString = true;
    private boolean enableURI = true;
    private boolean enableURL = true;

    /**
     * @see com.nhncorp.usf.core.interceptor.impl.InterceptorPropertyAware#setProperties(java.util.Properties)
     *      {@inheritDoc}
     */
    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    /**
     * @param actionInvocation the ActionInvocation
     * @return the interceptor result
     * @throws Exception for Exception
     */
    @SuppressWarnings("unchecked")
	public String intercept(ActionInvocation actionInvocation) throws Exception {
        /* if (properties == null || properties.size() == 0) {
            log.debug("property is blank. execute action invocation.");
            return actionInvocation.invoke();
        }
        */

        if (properties != null) {
            Enumeration names = properties.propertyNames();

            while (names.hasMoreElements()) {
                String varName = (String) names.nextElement();
                String valueObj = properties.getProperty(varName);

                if (StringUtil.isNotEmpty(varName) && StringUtil.isNotEmpty(valueObj)) {
                    boolean value = Boolean.valueOf(valueObj);

                    if (varName.equalsIgnoreCase(DEFAULT_PARAM_NAMES.usf_querystring.name())) {
                        enableQueryString = value;
                    } else if (varName.equalsIgnoreCase(DEFAULT_PARAM_NAMES.usf_uri.name())) {
                        enableURI = value;
                    } else if (varName.equalsIgnoreCase(DEFAULT_PARAM_NAMES.usf_url.name())) {
                        enableURL = value;
                    }
                }
            }
        }

        HttpServletRequest request = ServletHelper.getRequest(actionInvocation);
        String method = request.getMethod();
        String uri = request.getRequestURI();
        StringBuffer urlBuf = request.getRequestURL();
        String url = null;

        if (urlBuf != null) {
            url = urlBuf.toString();
        }

        String queryString = null;

        if (method.equalsIgnoreCase("GET")) {
            queryString = ServletRequestUtil.getQueryString(request);
        }

        queryString = StringUtil.isNotEmpty(queryString) ? queryString : "";
        Map<String, Object> dataMap = ActionInvocationHelper.getDataMap(actionInvocation);

        if (enableQueryString) {
            dataMap.put(DEFAULT_PARAM_NAMES.usf_querystring.name(), queryString);
        }

        if (enableURI) {
            dataMap.put(DEFAULT_PARAM_NAMES.usf_uri.name(), uri);
        }

        if (enableURL) {
            if (queryString == null) {
                dataMap.put(DEFAULT_PARAM_NAMES.usf_url.name(), url);
            } else {
                dataMap.put(DEFAULT_PARAM_NAMES.usf_url.name(), url + "?" + queryString);
            }
        }

        log.debug("QueryString : " + queryString + ", URI : " + uri + ", URL : " + url);
        return actionInvocation.invoke();
    }

}